package org.softmed.rest.admin.httpMethods;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Map;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

import org.softmed.rest.admin.DetailLevelListener;
import org.softmed.rest.admin.DetailChooser.DetailLevel;
import org.softmed.rest.admin.interfaces.EventListener;
import org.softmed.rest.admin.interfaces.FileHandler;
import org.softmed.rest.admin.interfaces.SelectionPathChanged;
import org.softmed.rest.admin.mediaType.MediaTypeEditor;
import org.softmed.rest.admin.mediaType.MediaTypeHandlerManager;
import org.softmed.rest.admin.module.ValidationException;

public class HTTPMethodEditor extends JPanel implements EventListener, SelectionPathChanged, DetailLevelListener {

	MediaTypeHandlerManager mediaTypeHandlerManager = new MediaTypeHandlerManager();
	HTTPMethodHeader methodHeader = new HTTPMethodHeader();
	FileHandler fileHandler;

	public HTTPMethodEditor() {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
//		setBackground(Color.GREEN);
		
//		setPreferredSize(new Dimension(540,25));
//		setMaximumSize(new Dimension(540,25));
		setMinimumSize(new Dimension(100,25));
		
		methodHeader.setListener(this);
		methodHeader.setDetailListener(this);
		
		
		add(methodHeader);
		add(mediaTypeHandlerManager);
//		add(Box.createVerticalGlue());
		detailLevelChanged(DetailLevel.MEDIA_TYPE);
	}

	Map<String, String> handlers;

	public void setMethodName(String text) {
		methodHeader.setMethodName(text);
	}

	
	public Map<String, String> getHandlers() {
		return handlers;
	}

	public void setHandlers(Map<String, String> handlers) {
		this.handlers = handlers;
		mediaTypeHandlerManager.setHandlers(handlers);
	}

	@Override
	public void listen(Object source) {
		mediaTypeHandlerManager.addNewMediaTypeHandler();
		detailLevelChanged(DetailLevel.MEDIA_TYPE);
	}


	public void validateData() throws ValidationException {
		mediaTypeHandlerManager.validateData();
	}


	public void selectionPathChanged(boolean directory, String name, String parent) {
		mediaTypeHandlerManager.selectionPathChanged(directory,name,parent);
		
	}


	public void setFileHandler(FileHandler fileHandler) {
		mediaTypeHandlerManager.setFileHandler(fileHandler);
	}


	@Override
	public void detailLevelChanged(DetailLevel detailLevel) {
		switch (detailLevel) {
		case METHODS:
			mediaTypeHandlerManager.setVisible(false);
			// TODO change methods manager detail view level
			mediaTypeHandlerManager.detailLevelChanged(detailLevel);
			methodHeader.detailLevelChanged(detailLevel);
			break;
		case MEDIA_TYPE:
			mediaTypeHandlerManager.setVisible(true);
			// TODO change methods manager detail view level
			mediaTypeHandlerManager.detailLevelChanged(detailLevel);
			methodHeader.detailLevelChanged(detailLevel);
			break;
		}
		
		
	}
}
